import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        meta: { title: "首页" },
        component: () => import('@/views/index'),
        children: [
            {
                path: '/',
                name:'home',
                meta: { title: "首页" },
                component: () => import('@/views/home/home'),
            },
            {
                path: '/goods',
                meta: { title: "商品类" },
                component: () => import('@/views/goods/router'),
                children: [
                    {
                        path: '/',
                        name: 'goods',
                        meta: { title: "商品" },
                        component: () => import('@/views/goods/index')
                    },
                    {
                        path: 'info/:id',
                        name: 'goods_info',
                        meta: { title: "商品详细" },
                        component: () => import('@/views/goods/info')
                    },
                ]
            },
        ]
    },
    // 用户类
    {
        path: '/user',
        meta: { title: "用户" },
        component: () => import('@/views/user/router'),
        children: [
            // 登录
            {
                path: 'login',
                name:'login',
                meta: { title: "用户 - 登录" },
                component: () => import('@/views/user/login'),
            },
            // 注册
            {
                path: 'reg',
                name:'reg',
                meta: { title: "用户 - 注册" },
                component: () => import('@/views/user/reg'),
            },
            // 找回密码
            {
                path: 'back',
                name:'back',
                meta: { title: "用户 - 找回密码" },
                component: () => import('@/views/user/back'),
            }
        ]
    },
    // 用户中心
    {
        path: '/user',
        meta: { title: "用户" },
        component: () => import('@/views/user/router2'),
        children: [
            // 个人中心
            {
                path: 'index',
                name: 'userhome',
                meta: { title: "用户中心" },
                component: () => import('@/views/user/user'),
                children: [
                    // 默认
                    {
                        path: '/',
                        name:'order',
                        meta: { title: "我的订单" },
                        component: () => import('@/views/user/index/order'),
                    },
                    // 我的订单
                    {
                        path: 'order',
                        name:'order',
                        meta: { title: "我的订单" },
                        component: () => import('@/views/user/index/order'),
                    },
                    // 售后服务
                    {
                        path: 'serve',
                        name:'serve',
                        meta: { title: "售后服务" },
                        component: () => import('@/views/user/index/serve'),
                    },
                    // 账户资料
                    {
                        path: 'data',
                        name:'data',
                        meta: { title: "账户资料" },
                        component: () => import('@/views/user/index/data'),
                    },
                    // 收货地址
                    {
                        path: 'address',
                        name:'address',
                        meta: { title: "收货地址" },
                        component: () => import('@/views/user/index/address'),
                    },
                    // 购物车
                    {
                        path: 'cart',
                        name:'cart',
                        meta: { title: "购物车" },
                        component: () => import('@/views/user/index/cart'),
                    },
                ]
            }
        ]
    },

]

const router = new VueRouter({
    mode:'history',
    routes
})

// 没设置标题就打不开
router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = to.meta.title
    }
    next()
})

export default router